package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.UmsAboutUsMapper;
import com.macro.mall.mapper.UmsFeedbackMapper;
import com.macro.mall.model.UmsAboutUs;
import com.macro.mall.model.UmsAboutUsExample;
import com.macro.mall.model.UmsFeedback;
import com.macro.mall.model.UmsFeedbackExample;
import com.macro.mall.service.AboutUsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台展示关于我们和意见反馈的service
 */
@Service
public class AboutUsServiceImpl implements AboutUsService {

    @Autowired
    private UmsAboutUsMapper aboutUsMapper;
    @Autowired
    private UmsFeedbackMapper feedbackMapper;

    @Override
    public CommonResult createAboutUs(UmsAboutUs aboutUs) {
        aboutUsMapper.deleteByExample(new UmsAboutUsExample());
        int count = aboutUsMapper.insertSelective(aboutUs);
        if(count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult<UmsAboutUs> getAboutUs() {
        UmsAboutUs result = new UmsAboutUs();
        List<UmsAboutUs> umsAboutUses = aboutUsMapper.selectByExample(new UmsAboutUsExample());
        if (umsAboutUses != null && umsAboutUses.size() > 0) {
            return CommonResult.success(umsAboutUses.get(0));
        } else {
            return CommonResult.success(result);
        }
    }

    @Override
    public CommonResult updateAboutUs(UmsAboutUs aboutUs) {
        int count = aboutUsMapper.update(aboutUs);
        if(count > 0){
            return CommonResult.success(count);
        }else{
            return CommonResult.failed();
        }
    }

    @Override
    public List<UmsFeedback> getFeedbackList(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UmsFeedback> result = new ArrayList<>();
        UmsFeedbackExample example = new UmsFeedbackExample();
        example.setOrderByClause("id desc");
        List<UmsFeedback> umsFeedbacks = feedbackMapper.selectByExampleWithBLOBs(example);
        if(umsFeedbacks != null && umsFeedbacks.size() > 0){
            return umsFeedbacks;
        }else {
            return result;
        }

    }

    @Override
    public int deleteFeedback(Long id) {
        return feedbackMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteFeedbackList(List<Long> ids) {
        UmsFeedbackExample example = new UmsFeedbackExample();
        example.createCriteria().andIdIn(ids);
        return feedbackMapper.deleteByExample(example);
    }
}
